﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace LogDispControl
{
    public partial class LogContainer : UserControl
    {
        internal float FontSize = 20f;

        private LogLineList LogLineList = new LogLineList();

        private float scrollValue = 0f;

        private bool isdisp = false;


        public int MaxCount
        {
            get
            {
                return LogLineList.MaxCount;
            }
            set
            {
                LogLineList.MaxCount = value;
            }
        }

        public LogContainer()
        {
            InitializeComponent();
            base.MouseWheel += LogContainer_MouseWheel;
        }

        private void LogContainer_MouseWheel(object sender, MouseEventArgs e)
        {
            int value = vScrollBar1.Value;
            value = ((e.Delta >= 0) ? (value - vScrollBar1.SmallChange) : (value + vScrollBar1.SmallChange));
            if (value >= vScrollBar1.Maximum - base.Height || value <= vScrollBar1.Minimum)
            {
                value = ((value < vScrollBar1.Maximum - base.Height) ? vScrollBar1.Minimum : (vScrollBar1.Maximum - base.Height));
            }
            vScrollBar1.Value = value;
        }

        public void DispInfo(Color color, string info)
        {
            if (base.InvokeRequired)
            {
                Invoke((Action)delegate
                {
                    DispInfo(color, info);
                });
            }
            else
            {
                LogLineList.Addline(color, info);
                isdisp = true;
                Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            PointF pointF = new PointF(0f, 0f);
            base.OnPaint(e);
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.TranslateTransform(0f, scrollValue);
            for (int i = 0; i < LogLineList.LogLines.Count; i++)
            {
                GraphicsPath graphicsPath = new GraphicsPath();
                graphicsPath.AddString(LogLineList.LogLines[i].Content, new FontFamily("黑体"), 1, FontSize, new RectangleF(pointF.X, pointF.Y, base.Width - vScrollBar1.Width, 100000f), new StringFormat());
                RectangleF bounds = graphicsPath.GetBounds();
                pointF = new PointF(0f, bounds.Y + bounds.Height);
                e.Graphics.FillPath(new SolidBrush(LogLineList.LogLines[i].LineColor), graphicsPath);
                vScrollBar1.SmallChange = (int)FontSize + 1;
            }
            if (pointF.Y > (float)base.Height)
            {
                vScrollBar1.Minimum = 0;
                vScrollBar1.Maximum = (int)pointF.Y + 20;
                vScrollBar1.LargeChange = base.Height;
            }
            else
            {
                vScrollBar1.Minimum = 0;
                vScrollBar1.Maximum = base.Height;
                vScrollBar1.LargeChange = base.Height;
            }
            if (isdisp)
            {
                isdisp = false;
                vScrollBar1.Value = vScrollBar1.Maximum - base.Height;
            }
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            Invalidate();
        }

        private void vScrollBar1_ValueChanged(object sender, EventArgs e)
        {
            scrollValue = -vScrollBar1.Value;
            Invalidate();
        }



       
    }
}